﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using University.Workshop.Model;
using System.Globalization;

namespace University.Workshop.Business
{
    public class TransactionSendManager : IDisposable
    {
        private Client ClientTransaction;

        public void TransactionSendManagerConnectClient()
        {
            //Client connection
             ClientTransaction = new Client();
        }
        //warning corrected with this ->
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                // dispose managed resources
                ClientTransaction.Dispose();
            }
            // free native resources
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        public int SendCredit(string fromUserName, string toUserName, long amountToSend, long actualAmount)
        {
            if (String.IsNullOrEmpty(fromUserName) == true ||
                String.IsNullOrEmpty(toUserName) == true ||
                amountToSend <= 0 ||
                actualAmount <= 0)
            {
                return(3);
            }

            if (String.Equals(toUserName, fromUserName) == true)
            {
                return(1);
            }
            else
            {
                //If the user name don't complete the character orders
                string UserNamePattern = "^[a-zA-Z0-9_-]{1,30}$";
                if (!Regex.IsMatch(toUserName, UserNamePattern))
                {
                    return (2);
                }
                else
                {
                    //Here the sentences if everything is correct or not  
                    int ResultTransaction = ClientTransaction.NewTransaction(toUserName, fromUserName, amountToSend, actualAmount);

                    switch (ResultTransaction)
                    {
                        case 0:
                            {
                                //If is correct, the programa show a message of succesfull
                                return (0);
                            }

                        case 1:
                            {
                                //If is fail, the programa show a message of failed
                                return (3);
                            }

                        case 2:
                            {
                                //UserNameToSend dont found
                                return(4);
                            }
                    }
                }
            }
            return (3);
        }
    }
}
